/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/04/08
\**********************/
#include "LuaBase.h"
#include <iostream>
using namespace std;

namespace DuiLib {
namespace LuaBase {

void PrintA(SDString str)
{
    printf("%s\n", str.c_str());
}

void PrintW(SDWString str)
{
    wprintf(L"%s\n", str.c_str());
}

int LuaPrintW(LuaState *L)
{
    LuaStack args(L);
    SDWString str = L"invalid character.";
    if (args[1].IsWString()){
        str = (LPCWSTR)args[1].GetWString();
    }
    PrintW(str);
    return 0;
}

int LuaPrintA(LuaState *L)
{
    LuaStack args(L);
    SDString str = "invalid character.";
    if (args[1].IsString()){
        str = args[1].GetString();
    }
    PrintA(str);
    return 0;
}

int LuaPrintU(LuaState *L)
{
    LuaStack args(L);
    SDString str = "invalid character.";
    if (args[1].IsString()){
        str = SDConvert::UToA(args[1].GetString());
    }
    PrintA(str);
    return 0;
}

void RegisterBase(LuaState* L)
{
    LuaObject base = L->GetGlobals();
#ifdef _UNICODE
    base.SetBoolean("_UNICODE", true);
#else
    base.SetBoolean("_UNICODE", false);
#endif
#ifdef _DEBUG
    base.SetBoolean("_DEBUG", true);
#else
    base.SetBoolean("_DEBUG", false);
#endif

    base.Register("print", LuaPrintU);
    base.Register("printu", LuaPrintU);
    base.Register("printa", LuaPrintA);
    base.Register("printw", LuaPrintW);
#ifdef _UNICODE
    base.Register("printt", LuaPrintW);
#else
    base.Register("printt", LuaPrintA);
#endif
}

void UnRegisterBase(LuaState* L)
{
    LuaObject base = L->GetGlobals();
    base.SetNil("print");
    base.SetNil("printu");
    base.SetNil("printa");
    base.SetNil("printw");
    base.SetNil("printt");
}

}
}